package cn.iocoder.yudao.module.wms.service.inventory;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.wms.controller.admin.stockdetail.vo.StockDetailSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.stockdetail.StockDetailDO;
import cn.iocoder.yudao.module.wms.service.stockdetail.StockDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * @Author: Ljx
 * @createTime: 2024年05月12日 11:43:09
 * @version: v1.0.0
 * @Description: 通用库存处理实现类
 */
@Service("1")
public class CommonInventoryHandle extends InventoryAbstract {

    @Resource
    private StockDetailService stockDetailService;

    @Override
    protected void handleIncreaseStockDetail(Long productId, Long warehouseId, Long positionId, BigDecimal qty, BigDecimal unitAmt, BigDecimal costAmt, String batchNo, List<String> serialNo, Long docId, String docCode, Integer sequence) {
        StockDetailSaveReqVO stockDetail = new StockDetailSaveReqVO().setProductId(productId).setWarehouseId(warehouseId).setProductId(productId).setTotalStockQty(qty)
                .setStockQty(qty).setDocId(docId).setDocCode(docCode).setUnitAmt(unitAmt).setCostAmt(costAmt).setSequence(sequence);
        stockDetailService.createStockDetail(stockDetail);
    }

    @Override
    protected void handleDecreaseStockDetail(Long productId, Long warehouseId, Long positionId, BigDecimal qty, String batchNo, List<String> serialNo, Long docId, String docCode) {
        // 按照先进先出的规则减少库存
        List<StockDetailDO> stockDetails = stockDetailService.getStockDetailByProductWarehousePosition(productId, warehouseId, positionId);
        // 排序
        stockDetails.sort((o1, o2) -> o1.getId().compareTo(o2.getId()));

        for (StockDetailDO stockDetail : stockDetails) {
            if (stockDetail.getStockQty().compareTo(qty.abs()) > 0) {
                stockDetailService.updateStockDetail(stockDetail.getId(), qty);
                break;
            } else if (stockDetail.getStockQty().compareTo(qty.abs()) == 0) {
                stockDetailService.deleteStockDetail(stockDetail.getId());
                break;
            } else {
                qty = qty.add(stockDetail.getStockQty());
                stockDetailService.deleteStockDetail(stockDetail.getId());
            }
        }
    }

    @Override
    protected void handleRevertIncreaseStockDetail(Long productId, Long warehouseId, Long positionId, BigDecimal qty, String batchNo, List<String> serialNo, Integer sequence) {
        // 先查找该产品的库存明细，若该明细未做出库，则删除该明细，如已做出库，则删除该明细后，依据先进先出原则，补齐数量
        StockDetailDO stockDetail = stockDetailService.getStockDetail(stockDetailId);
        stockDetailService.getStockDetail()
        BigDecimal shortQty = qty.abs();
        if (ObjectUtil.isNotEmpty(stockDetail)) {
            stockDetailService.deleteStockDetail(stockDetailId);
            if(stockDetail.getStockQty().compareTo(stockDetail.getTotalStockQty()) != 0) { //入库库存已被使用
                shortQty = stockDetail.getTotalStockQty().subtract(stockDetail.getStockQty());
            }
        }
        if (shortQty.compareTo(BigDecimal.ZERO) > 0) {
            handleDecreaseStockDetail(productId, warehouseId, positionId, shortQty, batchNo, serialNo, null, null);
        }
    }

    @Override
    protected void handleRevertDecreaseStockDetail(Long productId, Long warehouseId, Long positionId, BigDecimal qty, BigDecimal unitAmt, BigDecimal costAmt, String batchNo, List<String> serialNo, Long docId, String docCode) {
        handleIncreaseStockDetail(productId, warehouseId, positionId, qty, unitAmt, costAmt, batchNo, serialNo, docId, docCode, 0);
    }
}
